#include "ElementXML.h"

ElementXML::ElementXML(vector<NodeXML *> * _children, vector<AttributeXML *> * _attributes, string _ns, string _name, bool _isAutoClosed){
	children= _children;
	attributes= _attributes;
	name= _name;
	ns = _ns;
	isAutoClosed = _isAutoClosed;
	//daddy= _daddy;
}


ElementXML::ElementXML(vector<NodeXML *> * _children, vector<AttributeXML *> * _attributes, char* _ns, char* _name, bool _isAutoClosed){
	children= _children;
	attributes= _attributes;
	name= string(_name);
	if(_ns != NULL)
		 ns = string(_ns);
	isAutoClosed = _isAutoClosed;
}

bool ElementXML::setAttributes(vector<AttributeXML * > *_attributes){
	if(!(_attributes == NULL))
	{
		this->attributes = _attributes;
		return true;
	}
	else
		return false;
}
bool ElementXML::setChildren(vector<NodeXML * > *_children){
	if(!(_children == NULL))
	{
		this->children = _children;
		return true;
	}
	else
		return false;
}

bool ElementXML::addAttribute(AttributeXML * _at){
	if(_at == NULL)
		return false;
	else
	{
		this->attributes->push_back(_at);
		return true;
	}
}
bool ElementXML::addChild(NodeXML * _child){
	if(_child == NULL)
		return false;
	else
	{
		this->children->push_back(_child);
		return true;
	}
}
vector<AttributeXML *> * ElementXML::getAttributes() const{
	return this->attributes;
}
vector<NodeXML *> * ElementXML::getChildren() const{
	return this->children;
}


string ElementXML::toString() const{
	string out;
	//Show the tag with his list of attributes
	//Ex: <tag attributes1="hihi" attributes2="hoho" ... >
	out = "<";
	if(!ns.empty())
	{
		out.append(ns);	
		out.append(":");	
	}
	out.append((name));
	for(size_t i = 0; i<attributes->size(); i++)
	{
		out.append((*attributes)[i]->toString());
	}
	if(isAutoClosed)
	{
		out.append("/>");
		return out;
	}
 	out.append(">");
	out.append("\n");
	
	//Show the different child element (text or other elements)
	for(size_t i = 0; i<children->size(); i++)
	{
		out.append("\t"+ (*children)[i]->toString());
		out.append("\n");
	}

	out.append("</");
	if(!ns.empty())
	{
		out.append(ns);	
		out.append(":");	
	}
	out.append((name));
	out.append(">");
	return out;
}

string ElementXML::getName() const{
	return this->name;
}

bool ElementXML::setName(string _name){
	if(!_name.empty())
	{
		this->name = _name;
		return true;
	}
	else
		return false;
}


string ElementXML::getNameSpace() const{
	return this->ns;
}
bool ElementXML::setNameSpace(string _ns){
	if(!_ns.empty())
		{
			this->ns = _ns;
			return true;
		}
		else
			return false;
}

string ElementXML::getRegExp() const {
	return "<" + name;
}

string ElementXML::getRegExpChildren() const {
	string s;
	vector<NodeXML*>:: iterator it;
	/* for each child, append it to the regExp */
	for( it = children->begin() ; it != children->end() ; ++it)
	{
		s += (*it)->getRegExp();
	}

	return s;
}
